/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-07-26 15:15:12
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-08-22 17:16:58
 * @FilePath: /vue3-uniapp/src/service/useMy.ts
 */
import type { MyAreaEsListItem } from '@/typings/api/my';
import { readMyAreaEs, OrderSearchRequestParam } from '@/api/my';

import {
    createNotifyRequestChange,
    createUseRequestSubject
} from './useRequestSubject';

export { OrderSearchRequestParam };

export const notifyAreaEsListChange = createNotifyRequestChange(readMyAreaEs);

export const useAreaEsListSubscribe = createUseRequestSubject(readMyAreaEs);

export const useMy = () => {
    const areaEsList = ref<MyAreaEsListItem[]>([]);
    const areaEsTotal = ref(0);
    const areaEsLoading = ref(false);
    let areaEsListParam = new OrderSearchRequestParam();
    const getAreaEsList = (param: OrderSearchRequestParam) => {
        if (areaEsLoading.value) return Promise.reject();
        areaEsLoading.value = true;
        areaEsListParam = {
            ...areaEsListParam,
            ...param
        };
        return readMyAreaEs(areaEsListParam)
            .then((res) => {
                areaEsList.value = res?.data.records || [];
                areaEsTotal.value = res?.data.total || 0;
                return res;
            })
            .finally(() => {
                areaEsLoading.value = false;
            });
    };

    return {
        getAreaEsList,
        areaEsTotal,
        areaEsList,
        areaEsLoading
    };
};
